package com.cqjtu.education.service.impl;

import com.cqjtu.education.dao.UserDao;
import com.cqjtu.education.entity.StudentWork;
import com.cqjtu.education.entity.User;
import com.cqjtu.education.entity.Work;
import com.cqjtu.education.dao.WorkDao;
import com.cqjtu.education.service.WorkService;
import com.github.pagehelper.Page;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * (Work)表服务实现类
 *
 * @author makejava
 * @since 2022-04-20 15:51:08
 */
@Service("workService")
public class WorkServiceImpl implements WorkService {
    @Resource
    private WorkDao workDao;

    @Resource
    private UserDao userDao;

    /**
     * 通过ID查询单条数据
     *
     * @param workId 主键
     * @return 实例对象
     */
    @Override
    public Work queryById(String workId) {
        return this.workDao.queryById(workId);
    }

    @Override
    public List<StudentWork> queryByTaskId(String taskId) {
        List<StudentWork> studentWorks = new ArrayList<StudentWork>();
        List<Work> works = this.workDao.queryByTaskId(taskId);
        for (Work work : works) {
            User user = this.userDao.queryById(work.getStudentId());
            StudentWork studentWork = new StudentWork();
            studentWork.setStudentId(user.getId());
            studentWork.setWorkId(work.getWorkId());
            studentWork.setContent(work.getContent());
            studentWork.setIdentityId(user.getIdentityId());
            studentWork.setName(user.getRealName());
            studentWork.setScore(work.getScore());
            studentWorks.add(studentWork);
        }
        return studentWorks;
    }

    @Override
    public Work queryByStudentIdAndTaskId(String studentId, String taskId) {
        return this.workDao.queryByStudentIdAndTaskId(studentId, taskId);
    }


    /**
     * 新增数据
     *
     * @param work 实例对象
     * @return 实例对象
     */
    @Override
    public Work insert(Work work) {
        work.setWorkId(UUID.randomUUID().toString());
        this.workDao.insert(work);
        return work;
    }

    /**
     * 修改数据
     *
     * @param work 实例对象
     * @return 实例对象
     */
    @Override
    public Work update(Work work) {
        this.workDao.update(work);
        return this.queryById(work.getWorkId());
    }

    @Override
    public int mark(String workId, int score) {
        return this.workDao.mark(workId, score);
    }

    /**
     * 通过主键删除数据
     *
     * @param workId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String workId) {
        return this.workDao.deleteById(workId) > 0;
    }
}
